package view.sales;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.sales.SalesInit;
import utility.StringUtils;
import utility.com.TextComponentUtils;

public class SalesInitListTM extends AbstractTableModel {

    private List<SalesInit> data;

    public SalesInitListTM(List<SalesInit> data) {
        this.data = data;
    }

    public SalesInitListTM() {
        this(new ArrayList<SalesInit>(0));
    }

    public SalesInit get(int index) {
        return data.get(index);
    }

    public void add(SalesInit product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, SalesInit product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<SalesInit> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
         return 8;
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
           
            case 1: 
                if (data.get(rowIndex).getTransDate()!=null){
                    return StringUtils.formatDateTime24(data.get(rowIndex).getTransDate());
                } return null;
           case 2: 
                if (data.get(rowIndex).getCloseTime()!=null){
                    return StringUtils.formatDateTime24(data.get(rowIndex).getCloseTime());
                } return null;
                
            case 3: return data.get(rowIndex).getPcId();
            case 4: return data.get(rowIndex).getUserInserted();
            
            case 5: return data.get(rowIndex).getShiftId();
            case 6: 
                 String displaySub = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getModal(), 0);
                 return displaySub; 
        }
        return null;
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "TGL MULAI";
            case 2: return "TGL TUTUP";
            case 3: return "NO KASIR";
            case 4: return "OPERATOR";
            case 5: return "SHIFT";
            case 6: return "MODAL";
            default: return null;
        }
    }
    
}
